
var FB              = require('../../../fb'),
    Step            = require('step'),

    config          = require('../config');

FB.options({
    appId:          config.facebook.appId,
    appSecret:      config.facebook.appSecret,
    redirectUri:    config.facebook.redirectUri
});

exports.MySelf = function(req, res){
var uname = '';
 var categories = '';
 var likes = '';
FB.api('me/', {
        fields:         'id,name',
        limit:          250,
        access_token:   req.session.access_token
     }, function (userresult) {
        if(!userresult || userresult.error) {
            return res.send(500, 'error');
        }
      uname = userresult.name;

FB.api('/me/likes', {
        fields:         'name,category',
        limit:          250,
        access_token:   req.session.access_token
    }, function (result) {
        if(!result || result.error) {
            return res.send(500, 'error');
        }

for(i=1; i<=result.data.length; i++)
{
        var catg = '';
        var like = '';
        catg = result.data[i-1].category;
        like = result.data[i-1].name;
	categories = categories + catg + '<br>';
	likes = likes + like + '<br>';
}

	var p = {uname : uname, categories : categories, likes : likes};
        return res.send(p);

});
});
};

exports.MyLikes = function(req, res)
{
	var likes = '';
	var uname =  req.param('uname');
	var ucategory =  req.param('ucategory');
	var categories = '';
	
		
		FB.api('me/likes',{  fields:'name,id,category',  	limit:5000, access_token:   	req.session.access_token},function(resp){
		for(i=1; i<=resp.data.length; i++)
		{
        	var catg = '';
        	var like = '';
        	catg = resp.data[i-1].category;
        	like = resp.data[i-1].name;
		
		if(catg == null)
			catg = '';
		if(like == null)
			like = '';

		if(ucategory == 'ALL' || catg.indexOf(ucategory) != -1)
		{
			likes = likes + like + '<br>';
		}
		}
		var p = {flikes: likes};
        	return res.send(p);
		});
};

exports.Friends = function(req, res)
{
	var friends = '';
	var categories = '';
	FB.api("me/friends",{  fields:'name,id,user_likes',  limit:5000, access_token:   	req.session.access_token},function(resv){
	var data = resv.data;
	for(j=0; j < data.length; j++){
	var temp = '';
	temp = data[j].name;

	friends = friends + temp + '<br>';
		var likes = data[j].user_likes;
		
		if(likes)
		{
		for(a in likes.data)
		{
			for(l=0; l < a.length; l++){
			categories = categories + likes.data[a[l]].category + '<br>';
			}
		}
		}
		categories = categories + '<br/>';
	}
        
	var p = {friends : friends, categories : categories};
        return res.send(p);
});
};

exports.FLikes = function(req, res)
{
	var likes = '';
	var uname =  req.param('uname');
	var ucategory =  req.param('ucategory');
	var categories = '';
	FB.api("me/friends",{  fields:'name,id',  limit:5000, access_token:   	req.session.access_token},function(resv){

	var data = resv.data;

	for(j=0; j < data.length; j++){
	var temp = '';
	temp = data[j].name;
	if(temp == null)
		temp = '';
	if(uname.indexOf(temp) != -1)
	{
		FB.api(data[j].id + "/likes",{  fields:'name,id,category',  	limit:5000, access_token:   	req.session.access_token},function(resp){
		for(i=1; i<=resp.data.length; i++)
		{
        	var catg = '';
        	var like = '';
        	catg = resp.data[i-1].category;
        	like = resp.data[i-1].name;
		
		if(catg == null)
			catg = '';
		if(like == null)
			like = '';

		if(ucategory == 'ALL' || ucategory.indexOf(catg) != -1)
		{
			likes = likes + like + '<br>';
		}
		}
		var p = {flikes: likes};
        	return res.send(p);
		});
	}
	}
	});
};

exports.index = function(req, res) {
    var accessToken = req.session.access_token;
    if(!accessToken) {
        
res.render('index', {
            title: 'Express',
            loginUrl: FB.getLoginUrl({ scope: 'user_about_me'})
        });
} else {
  res.render('likes');
}
};

exports.loginCallback = function (req, res, next) {
    var code            = req.query.code;

    if(req.query.error) {
        // user might have disallowed the app
        return res.send('login-error ' + req.query.error_description);
    } else if(!code) {
        return res.redirect('/');
    }

    Step(
        function exchangeCodeForAccessToken() {
            FB.napi('oauth/access_token', {
                client_id:      FB.options('appId'),
                client_secret:  FB.options('appSecret'),
                redirect_uri:   FB.options('redirectUri'),
                code:           code
            }, this);
        },
        function extendAccessToken(err, result) {
            if(err) throw(err);
            FB.napi('oauth/access_token', {
                client_id:          FB.options('appId'),
                client_secret:      FB.options('appSecret'),
                grant_type:         'fb_exchange_token',
                fb_exchange_token:  result.access_token
            }, this);
        },
        function (err, result) {
            if(err) return next(err);

            req.session.access_token    = result.access_token;
            req.session.expires         = result.expires || 0;

            if(req.query.state) {
                var parameters              = JSON.parse(req.query.state);
                parameters.access_token     = req.session.access_token;

                console.log(parameters);

                       } else {
                return res.redirect('/');
            }
        }
    );
};

exports.logout = function (req, res) {
    req.session = null; // clear session
    res.redirect('/');
};